package com.it.share;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class App {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext content = new AnnotationConfigApplicationContext(AppConfig.class);
        JdbcTemplate jdbcTemplate = content.getBean(JdbcTemplate.class);

        for (int i = 0; i < 4; i++) {
            int id = RandomUtil.randomInt(10, 10000);
            String name = RandomUtil.randomString(4);
            jdbcTemplate.execute(String.format("insert into user(id,name) values (%d,'%s')", id, name));
        }

        for (int a = 0; a < 2; a++) {
            List<User> list = jdbcTemplate.query("select id,name from user", (rs, rowNum) -> {
                int i = rs.getInt(1);
                String n = rs.getString(2);
                return new User(i, n);
            });

            System.out.println("===================");
            System.out.println(JSONUtil.toJsonPrettyStr(list));
            System.out.println("===================");
        }


    }

    @Data
    @AllArgsConstructor
    static class User {
        private Integer id;
        private String name;
    }


}
